<?php

/**
 * ajax操作
 *
 */
class User_SayController extends Fengqiaoju_Controller_Action {

	public function init() {

		parent::init ();
		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ();
		$this->sayObject = new User_Models_Say ();
		
		if (! $this->isLogin ()) {
			exit ( '请登入' );
		}
	}

	/**
	 * 添加内容
	 */
	public function addAction() {

		$content = $this->_request->getPost ( 'content' );
		if (strlen ( $content ) > 1) {
			$data ['say_inputtime'] = date ( 'Y-m-d H:i:s' );
			$data ['say_content'] = $content;
			$data ['say_uid'] = $this->userObject->getUserId ();
			$data ['say_type'] = $this->_request->getPost ( 'type' );
			if ($this->sayObject->insert ( $data )) {
				echo 1;
			}
			else {
				echo 0;
			}
		}
		else {
			echo 0;
		}
	}

	/**
	 * 获取内容
	 */
	public function getAction() {

		$count = 10;
		
		$type = $this->_request->get ( 'type' );
		$uid = $this->_request->get ( 'uid' );
		$order = $this->_request->get ( 'order' );
		$page = $this->_request->get ( 'page' );
		
		$where = '1';
		if ($uid == 'all') {
		
		}
		elseif (! $uid) {
			$where = $where . " AND say_uid=" . $this->userObject->getUserId ();
		}
		else {
			$where = $where . " AND say_uid=" . $uid;
		}
		
		if ($type) {
			$where = $where . " AND say_type='$type'";
		}
		else {
			$where = $where . " AND say_type='心情'";
		}
		
		if ($order == 'up') {
			$order = 'say_inputtime';
		}
		else {
			$order = 'say_inputtime DESC';
		}
		
		$content = $this->sayObject->fetchAll ( $where, $order, $count, $offset );
		if (count ( $content )) {
			foreach ( $content as $key => $val ) {
				$result [$key] ['username'] = $this->userObject->getUserName ( $val ['say_uid'], 1 );
				$result [$key] ['info'] = "<a hre='#'>" . $result [$key] ['username'] . "</a>的" . $val ['say_type'] . ":" . $val ['say_content'];
				$result [$key] ['uid'] = $val ['say_uid'];
				$result [$key] ['time'] = $val ['say_inputtime'];
				$result [$key] ['avatar'] = "<img src='" . $this->userObject->getAvatar ( 'small', $val ['say_uid'] ) . "' />";
			}
		}
		else {
			$result [0] ['info'] = '暂无数据';
			$result [0] ['uid'] = $this->userObject->getUserId ();
			$result [0] ['username'] = $this->userObject->getUserName ();
			$result [0] ['avatar'] = "<img src='" . $this->userObject->getAvatar ( 'small', $this->userObject->getUserId () ) . "' />";
		}
		
		echo Zend_Json::encode ( $result );
	}

}